package example.cloudsitemanagement8002.controller;

import com.github.pagehelper.PageInfo;
import example.cloudsitemanagement8002.dto.SiteInfoQueryDTO;
import example.cloudsitemanagement8002.entity.SiteInfo;
import example.cloudsitemanagement8002.service.SiteInfoService;
import example.cloudsitemanagement8002.util.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/siteInfo")
public class SiteInfoController {

    @GetMapping("/{id}")
    public CommonResult<SiteInfo> getSiteInfoById(@PathVariable Long id) {
        log.info("Instance on port {} handling GET request for /siteInfo/{} with id: {}", serverPort, id, id); // Corrected log message
        if (id == null) {
            return CommonResult.failed("ID 不能为空");
        }
        SiteInfo siteInfo = siteInfoService.getSiteById(id);
        if (siteInfo != null) {
            return new CommonResult<>(200, "查询场地信息成功, 服务端口: " + serverPort, siteInfo);
        }
        return CommonResult.failed("查询的站点信息不存在, 服务端口: " + serverPort);
    }
    private static final Logger log = LoggerFactory.getLogger(SiteInfoController.class);

    @Value("${server.port}")
    private String serverPort;
    @Autowired
    private SiteInfoService siteInfoService;
    @PostMapping("/selectSiteInfoList")
    public CommonResult<PageInfo<SiteInfo>> listSites(@RequestBody(required = false) SiteInfoQueryDTO queryDTO) {
        log.info("Request received on port: " + serverPort + " for listSites with query: " + queryDTO);
        if (queryDTO == null) {
            return CommonResult.failed("参数不能为空，必须为JSON格式且包含pageNum、pageSize字段");
        }
        if (queryDTO.getPageNum() == null || queryDTO.getPageSize() == null) {
            return CommonResult.failed("pageNum和pageSize不能为空");
        }
        PageInfo<SiteInfo> pageInfo = siteInfoService.listSites(queryDTO);
        return CommonResult.success(pageInfo);
    }
    @GetMapping("/selectSiteInfoList")
    public CommonResult<PageInfo<SiteInfo>> listSiteOptions(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        log.info("Instance on port {} handling GET request for /siteInfo/selectSiteInfoList with pageNum: {}, pageSize: {}", serverPort, pageNum, pageSize);
        PageInfo<SiteInfo> pageInfo = siteInfoService.listSiteOptions(pageNum, pageSize);
        return CommonResult.success(pageInfo);
    }
    @PostMapping("/insertSiteInfo")
    public CommonResult<?> addSite(@RequestBody SiteInfo siteInfo) {
        log.info("Instance on port {} handling POST request for /siteInfo/insertSiteInfo with siteInfo: {}", serverPort, siteInfo);
        boolean success = siteInfoService.addSite(siteInfo);
        return success ? CommonResult.successMessage("添加成功") : CommonResult.failed("添加失败");
    }
    @PostMapping("/updateSiteInfoById")
    public CommonResult<?> updateSite(@RequestBody SiteInfo siteInfo) {
        log.info("Instance on port {} handling POST request for /siteInfo/updateSiteInfoById with siteInfo: {}", serverPort, siteInfo);
        if (siteInfo.getId() == null) return CommonResult.failed("ID不能为空");
        boolean success = siteInfoService.updateSite(siteInfo);
        return success ? CommonResult.successMessage("更新成功") : CommonResult.failed("更新失败");
    }
    @PostMapping("/deleteSiteInfoById")
    public CommonResult<?> deleteSite(@RequestBody Map<String, Long> payload) {
        log.info("Instance on port {} handling POST request for /siteInfo/deleteSiteInfoById with payload: {}", serverPort, payload);
        Long id = payload.get("id");
        if (id == null) return CommonResult.failed("ID不能为空");
        boolean success = siteInfoService.deleteSite(id);
        return success ? CommonResult.successMessage("删除成功") : CommonResult.failed("删除失败");
    }

}
